#include "render_system/gpu_info.h"

#include "render_system/gl_header.h"
#include "utility.h"

std::string GpuInfo::renderer()
{
    const GLubyte* str = glGetString(GL_RENDERER);
    return (char*)str;
}

std::string GpuInfo::vendor()
{
    const GLubyte *str = glGetString(GL_VENDOR);
    return (char*)str;
}

std::string GpuInfo::version()
{
    const GLubyte* str = glGetString(GL_VERSION);
    return (char*)str;
}

std::string GpuInfo::glsl_version()
{
    const GLubyte *str = glGetString(GL_SHADING_LANGUAGE_VERSION);
    return (char*)str;
}

size_t GpuInfo::major_version()
{
    GLint major;
    glGetIntegerv(GL_MAJOR_VERSION, &major);
    return major;
}

size_t GpuInfo::minor_version()
{
    GLint minor;
    glGetIntegerv(GL_MINOR_VERSION, &minor);
    return minor;
}


void GpuInfo::printGpuInfo()
{
    printf(
        Cyan,
        "vender      : %s\n"
        "renderer    : %s\n"
        "version     : %s\n"
        "glsl version: %s\n"

        , vendor().c_str()
        , renderer().c_str()
        , version().c_str()
        , glsl_version().c_str());
}

std::string GpuInfo::getError()
{
    GLenum err = glGetError();
    std::string errname;

#   define _CASE_BRANCH(val) case val: errname = #val; break

    switch(err)
    {
        _CASE_BRANCH(GL_NO_ERROR);
        _CASE_BRANCH(GL_INVALID_ENUM);
        _CASE_BRANCH(GL_INVALID_VALUE);
        _CASE_BRANCH(GL_INVALID_OPERATION);
        _CASE_BRANCH(GL_STACK_OVERFLOW);
        _CASE_BRANCH(GL_STACK_UNDERFLOW);
        _CASE_BRANCH(GL_OUT_OF_MEMORY);
        _CASE_BRANCH(GL_TABLE_TOO_LARGE);
    default:
        errname = "GL_NO_ERROR";
    }

    return errname;
}
